﻿using FluentValidation;
using Sgr.Indentity.Application.Commands;
using Sgr.Utilities;

namespace Sgr.Indentity.Application.Validations
{
    public class SendVerificationCodeCommandValidator : AbstractValidator<SendVerificationCodeCommand>
    {
        public SendVerificationCodeCommandValidator()
        {
            // 验证接收地址格式
            RuleFor(command => command.Receiver)
                .NotEmpty()
                .WithMessage("接收地址不能为空");

            // 用途
            RuleFor(command => command.Purpose)
                .NotEmpty()
                .WithMessage("用途不能为空")
                .Length(4)
                .WithMessage("用途必须是4位字符");

            // 手机号码格式验证
            When(command => command.ReceiverType == Security.VerifyCode.ReceiverType.Phone, () =>
            {
                RuleFor(command => command.Receiver)
                    .Must(phone => RegexHelper.IsValidPhoneNumber(phone))
                    .WithMessage("请输入有效的手机号码");
            });

            // 电子邮箱格式验证
            When(command => command.ReceiverType == Security.VerifyCode.ReceiverType.Email, () =>
            {
                RuleFor(command => command.Receiver)
                    .Must(email => RegexHelper.IsValidEmail(email))
                    .WithMessage("请输入有效的电子邮箱地址");
            });
        }
    }
}